#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;

void selectionSort(int*, int);//选择排序
void swap(int*, int, int);//交换函数
void printArray(int*, int);//数组打印函数，打印格式模仿 Java 的 Array.toString() 方法
void copyArray(int*, int*, int);//把参数2的数组赋值给参数1，要求两个数组长度相等
bool isEqule(int*, int*, int);//判断两个数组是否相等，要求传入的数组长度相等
int* generateRandomArray(int, int);
void comparator(int*, int);

int main() {
    srand(time(0));
    //int length = int((9999 + 1) * (rand() % (9999 + 1) / (double)(9999 + 1)));
    for (int i = 0;; i++) {
        cout << "第" << i + 1 << "次测试：" << endl;
        int length = 6;
        int maxValue = rand();
        int* arr1 = generateRandomArray(length, maxValue);
        int* arr2 = new int[length];
        copyArray(arr2, arr1, length);
        printArray(arr1, length);
        printArray(arr2, length);
        cout << (isEqule(arr1, arr2, length) ? "正确" : "错误") << endl;
    }
    return 0;
}

void selectionSort(int* arr, int n) {
    if (arr == NULL || n < 2) {
        return;
    }

    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
            if (minIndex != i) {
                swap(arr, i, minIndex);
            }
        }
    }
}

void swap(int* arr, int i, int j) {
    arr[i] = arr[i] ^ arr[j];
    arr[j] = arr[i] ^ arr[j];
    arr[i] = arr[i] ^ arr[j];
}

void printArray(int* arr, int n) {
    cout << "[";
    for (int i = 0; i < n; i++) {
        if (i != n - 1) {
            cout << arr[i] << ", ";
        } else {
            cout << arr[i];
        }
    }
    cout << "]" << endl;
}

int* generateRandomArray(int length, int maxValue) {
    srand(time(0));
    int* arr = new int[int((length + 1) * rand()%(9999+1)/(double)(9999+1))];
    for (int i = 0; i < length; i++) {
			arr[i] = (int) ((maxValue + 1) * (rand() % (9999 + 1) / (double)(9999 + 1))) - (int) (maxValue * (rand() % (9999 + 1) / (double)(9999 + 1)));
	}
    return arr;
}

void copyArray(int* destArr, int* srcArr, int n) {
    for (int i = 0; i < n; i++) {
        destArr[i] = srcArr[i];
    }
}

bool isEqule(int* arr1, int* arr2, int n) {
    for (int i = 0; i < n; i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

void comparator(int* arr, int n) {
    sort(arr, arr+n);
}